/*****************************************************************
Wowengine - v0.1

File: wen_transformation.cpp
Creation date: 2012-10-15
*****************************************************************/

#include "wen_transformation.h"

using namespace wen;

Transformation::Transformation()
:
m_eMatrixMode(VIEW)
{
	D3DXMatrixIdentity(&m_totalMatrix);
	D3DXMatrixTranslation(&m_translationMatrix, 0.0f, 0.0f, 0.0f);
	D3DXMatrixRotationZ(&m_rotationMatrix, 0.0f);
	D3DXMatrixScaling(&m_scaleMatrix, 1.0f, 1.0f, 1.0f);
}

Transformation::~Transformation()
{
}

void Transformation::SetMatrixMode(MatrixMode matrixMode)
{
	m_eMatrixMode = matrixMode;
}

MatrixMode Transformation::GetMatrixMode() const
{
	return m_eMatrixMode;
}

void Transformation::LoadIdentity()
{
	D3DXMatrixIdentity(&m_totalMatrix);
	
	if (m_eMatrixMode == VIEW) 
	{
		D3DXVECTOR3 eyePos(0.0f, 0.0f, -1.0f);
		D3DXVECTOR3 lookPos(0.0f, 0.0f, 0.0f);
		D3DXVECTOR3 upVector(0.0f, 1.0f, 0.0f);
		
		D3DXMatrixLookAtLH(&m_totalMatrix, &eyePos, &lookPos, &upVector);
	}
}

void Transformation::Translate(float fPosX, float fPosY, float fPosZ)
{
	D3DXMatrixTranslation(&m_translationMatrix, fPosX, fPosY, fPosZ);
}

void Transformation::Rotate(float fAngleX, float fAngleY, float fAngleZ)
{
	//D3DXMatrixRotationZ(&m_rotationMatrix, fAngle);
	//D3DXMatrixRotationY(&m_rotationMatrix, fAngle * 3.14159f / 180.0f);

	D3DXMatrixRotationYawPitchRoll(&m_rotationMatrix, fAngleX * 3.14159f / 180.0f, fAngleY * 3.14159f / 180.0f, fAngleZ * 3.14159f / 180.0f);
}

void Transformation::Scale(float fWidth, float fHeight, float fDepth)
{
	D3DXMatrixScaling(&m_scaleMatrix, fWidth, fHeight, fDepth);
}

const D3DXMATRIX& Transformation::GetTranslationMatrix() const
{
	return m_translationMatrix;
}

const D3DXMATRIX& Transformation::GetRotationMatrix() const
{
	return m_rotationMatrix;
}

const D3DXMATRIX& Transformation::GetScaleMatrix() const
{
	return m_scaleMatrix;
}

const D3DXMATRIX& Transformation::GetTotalTransformMatrix() const
{
	return m_totalMatrix;
}

void Transformation::CalculateAndSetMatrixBetweenTransforms(const Transformation& kTransformationOne, const Transformation& kTransformationTwo)
{
	//Rotation
	D3DXMATRIX rotationMatrix;
	D3DXMatrixMultiply(&rotationMatrix, &kTransformationOne.GetRotationMatrix(), &kTransformationTwo.GetRotationMatrix());
	m_rotationMatrix = rotationMatrix;

	//Scale
	D3DXMATRIX scaleMatrix;
	D3DXMatrixMultiply(&scaleMatrix, &kTransformationOne.GetScaleMatrix(), &kTransformationTwo.GetScaleMatrix());
	m_scaleMatrix = scaleMatrix;

	//Translation
	D3DXMATRIX translationMatrix;
	D3DXMatrixMultiply(&translationMatrix, &kTransformationOne.GetTranslationMatrix(), &kTransformationTwo.GetTranslationMatrix());
	m_translationMatrix = translationMatrix;

	//Multiply between rotation and scale matrix
	D3DXMATRIX rotationPerScaleMatrix;
	D3DXMatrixMultiply(&rotationPerScaleMatrix, &m_rotationMatrix, &m_scaleMatrix);

	//Multiply between rotationScale matrix and tranlation matrix, and save into the total matrix
	D3DXMatrixMultiply(&m_totalMatrix, &rotationPerScaleMatrix, &m_translationMatrix);
}

void Transformation::SetTotalTransformMatrixFromArray(const float* theArray)
{
	m_totalMatrix = D3DXMATRIX(theArray);
}